const http = require('http');
const fs = require('fs');
const path = require('path');
const mysql = require('../../node_modules/mysql');
const url = require('url');
let config = {
    "host":'192.168.111.144',
    "user":"root",
    "password":"ddd",
    "database":"mydatabase"
}
let con = mysql.createConnection(config);
con.connect();
http.createServer((req,res) =>{
    let myUrl = new URL(req.url,'http://localhost:8084')
    let pathName = myUrl.pathname;

    let filePath = '../chapterList.html';
    // console.log(pathName);
    let juageArr = pathName.split('/');
    // console.log(pathName)
    // console.log(juageArr)
    // console.log(req.url)
    if (pathName == '/') {
        fs.readFile(filePath,(e,file) => {
            if (e) {
                console.log(e)
            }
            else{
                res.writeHead(200,{"Content-Type":"text/html;charset=utf8"});
                res.write(file);
                res.end();
            }
        })
    }
    
    else if (juageArr[1] == "css") {
        let cssFilePath='..'+pathName;
        // console.log("cssfilepath:",cssFilePath)
        fs.readFile(cssFilePath,(e,file) => {
            if (e) {
                console.log(e);
            }
            else{
                res.writeHead(200,{"Content-Type":"text/css;charset=utf8"});
                res.write(file);
                res.end();
            }
        })
    }
    else if (juageArr[1] == "js") {
        let jsFilePath='./'+juageArr[2];
        fs.readFile(jsFilePath,(e,file) => {
            if (e) {
                console.log(e)
            }
            else{
                res.writeHead(200,{"Content-Type":"text/plain;charset=utf8"});
                res.write(file);
                res.end();
            }
        })
    }
    else if (juageArr[1] == "images") {
        let jsFilePath='..'+pathName;
        fs.readFile(jsFilePath,(e,file) => {
            if (e) {
                console.log(e)
                res.end();
            }
            else{
                res.writeHead(200,{"Content-Type":"image/jpg;charset=utf8"});
                res.write(file);
                res.end();
            }
        })
    }
    else if (juageArr[1].indexOf('detail')!=-1) {
        let detailFilePath = '../chapter.html';
        fs.readFile(detailFilePath,(e,file) => {
            if (e) {
                console.log(e)
                res.end('404 Not Found');
            }
            else{
                res.writeHead(200,{"Content-Type":"text/html;charset=utf8"});
                res.write(file);
                res.end();
            }
        })
    }
    else if (juageArr[1].indexOf('getDetail')!=-1) {
        console.log('Id',myUrl.search.split('=')[1])
        let needId = myUrl.search.split('=')[1];
        let sqltext = 'select * from blog where id=?';
        con.query(sqltext,[needId],(e , result) => {
            if (e) {
                res.writeHead(200,{"Content-Type":"text/json;charset=utf8"});
                res.end('503');
            }
            else {
                let sendData = JSON.stringify(result);
                console.log('type:::',typeof sendData)
                res.writeHead(200,{"Content-Type":"text/json;charset=utf8"});
                
                res.end(sendData);
            }
        })
    }
    else if (juageArr[1]='/getChapterList') {
        let sqltext = 'select * from blog';
        con.query(sqltext,[],(e,result) => {
            if (e) {
                console.log(e)
                res.writeHead(200,{"Content-Type":"text/json;charset=utf8"});
                res.end(JSON.stringify([]))
            }
            else{
                res.writeHead(200,{"Content-Type":"text/json;charset=utf8"});
                // console.log(result)
                let sendData = JSON.stringify(result);
                // console.log(sendData);
                res.write(sendData);
                res.end()
            }
        });
        
    }
    else{
        return
    }

}).listen(8084);
console.log("server is listening 8084")